package com.genesis.web.controller.usermember;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.UmsMemberReceiveAddress;
import com.genesis.mall.service.IUmsMemberReceiveAddressService;
import com.genesis.mall.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import jdk.nashorn.internal.parser.Token;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员收货地址表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/member/address")
public class UmsMemberReceiveAddressController {

    @Autowired
    private IUmsMemberReceiveAddressService iUmsMemberReceiveAddressService;


    @GetMapping("/list")
    public Result<List<UmsMemberReceiveAddress>> list(HttpServletRequest request) {

        String authorization = request.getHeader("Authorization");
        String substring = authorization.substring(6);

        Claims claims = TokenUtil.getClaims(substring);

        Long umsMemberId = Long.parseLong(claims.get("umsMember").toString());

        LambdaQueryWrapper<UmsMemberReceiveAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(umsMemberId.toString()), UmsMemberReceiveAddress::getMemberId, umsMemberId);

        List<UmsMemberReceiveAddress> list = iUmsMemberReceiveAddressService.list(queryWrapper);
        return Result.success(list, "操作成功");

    }

    @PostMapping("/add")
    @Transactional
    public Result<Integer> add(@RequestBody UmsMemberReceiveAddress umsMemberReceiveAddress, HttpServletRequest request) {

        iUmsMemberReceiveAddressService.updateAll();

        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(6);

        Claims claims = TokenUtil.getClaims(token);
        Long umsMemberId = Long.parseLong(claims.get("umsMember").toString());

        UmsMemberReceiveAddress umsMemberReceiveAddress1 = new UmsMemberReceiveAddress();
        BeanUtils.copyProperties(umsMemberReceiveAddress, umsMemberReceiveAddress1);
        umsMemberReceiveAddress1.setMemberId(umsMemberId);

        iUmsMemberReceiveAddressService.save(umsMemberReceiveAddress1);

        return Result.success(1, "操作成功");
    }

    @GetMapping("/{id}")
    public Result<UmsMemberReceiveAddress> getById(@PathVariable Long id) {


        UmsMemberReceiveAddress ura = iUmsMemberReceiveAddressService.getById(id);

        return Result.success(ura, "操作成功");

    }

    /**
     * 修改数据
     *
     * @param id
     * @return
     */

    @PostMapping("/update/{id}")
    @Transactional
    public Result<Integer> update(@PathVariable Long id, @RequestBody UmsMemberReceiveAddress umsMemberReceiveAddress) {
        if (umsMemberReceiveAddress.getDefaultStatus() == 1) {
            iUmsMemberReceiveAddressService.updateAll();
        }
        umsMemberReceiveAddress.setDefaultStatus(1);
        iUmsMemberReceiveAddressService.updateById(umsMemberReceiveAddress);
        return Result.success(1, "操作成功");

    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @Transactional
    public Result<String> delete(@PathVariable Long id) {

        UmsMemberReceiveAddress umsMemberReceiveAddress = iUmsMemberReceiveAddressService.getById(id);
        if (umsMemberReceiveAddress.getDefaultStatus() == 1) {
            return Result.error("默认地址无法删除");
        }

        iUmsMemberReceiveAddressService.removeById(id);
        return Result.success("1", "操作成功");


    }


}
